// pages/barbecue/examvip/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData:[],
    canvasHidden: true,     //设置画板的显示与隐藏，画板不隐藏会影响页面正常显示
    avatarUrl: '',         //用户头像
    nickName: '',          //用户昵称
    wxappName: "zhixingjiaoyue",    //小程序名称
    shareImgPath: '',
    screenWidth: '',       //设备屏幕宽度
    description: "ksdfwerwer",    //奖品描述
    FilePath: '',           //头像路径

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
    wx.setNavigationBarTitle({
      title: "查看考生信息",
    })
    var _this = this;
    var optdata = JSON.parse(options.dataset);
    var submitObj={
      examId: optdata.examid
    };
    app.paiRequest("/app/examinate/form", submitObj).then(function (res) {
      console.log(res.data)
      _this.setData({
        formData: res.data
      })
    })
    wx.getSystemInfo({
      success: res => {
        _this.setData({
          screenWidth: res.screenWidth,
          screenHeight: res.windowHeight
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  saveImageToPhotosAlbum:
    function () {
      wx.showLoading({
        title: '保存中...',
      })
      var that = this;
      //设置画板显示，才能开始绘图
      that.setData({
        canvasHidden: false
      })
      var unit = that.data.screenWidth
      var height = that.data.screenHeight
      var path1 = "/images/21.png"
      var avatarUrl = that.data.avatarUrl
      console.log(avatarUrl + "头像")
      var path2 = "/images/21.png"
      var path3 = "/images/21.png"
      var path4 = "/images/21.png"
      var path5 = "/images/21.png"
      var unlight = "/images/21.png"
      var nickName = that.data.nickName
      console.log(nickName + "昵称")
      var context = wx.createCanvasContext('share')
      var description = that.data.description
      var wxappName = "来「 " + that.data.wxappName + " 」试试运气"
      
      
      // context.drawImage(path2, unit * 48, unit * 120, unit * 280, unit * 178)
      // context.drawImage(path5, unit * 48, unit * 120, unit * 280, unit * 178)
      // context.drawImage(unlight, unit * 82, unit * 145, unit * 22, unit * 32)
      // context.drawImage(unlight, unit * 178, unit * 145, unit * 22, unit * 32)
      // context.drawImage(unlight, unit * 274, unit * 145, unit * 22, unit * 32)
      // context.drawImage(unlight, unit * 82, unit * 240, unit * 22, unit * 32)
      // context.drawImage(unlight, unit * 178, unit * 240, unit * 22, unit * 32)
      // context.drawImage(unlight, unit * 274, unit * 240, unit * 22, unit * 32)
      // context.drawImage(path3, unit * 20, unit * 385, unit * 55, unit * 55)
     
      
      
      context.setFontSize(30)
      context.setFillStyle("black")
      context.fillText("2015年反假货币考试", 20, 40)
      context.fillText("准考证", 50, 70)
      context.setLineWidth(1);
      context.moveTo(0,80)
      context.lineTo(unit,80);
      context.stroke()
      context.setLineWidth(1);
      context.moveTo(0, 80)
      context.lineTo(0, height);
      context.stroke()
      //把画板内容绘制成图片，并回调 画板图片路径
      context.draw(false, function () {
        wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          width: unit,
          height: height,
          destWidth: unit,
          destHeight: height,
          canvasId: 'share',
          success: function (res) {
            that.setData({
              shareImgPath: res.tempFilePath
            })
            if (!res.tempFilePath) {
              wx.showModal({
                title: '提示',
                content: '图片绘制中，请稍后重试',
                showCancel: false
              })
            }
            console.log(that.data.shareImgPath)
            //画板路径保存成功后，调用方法吧图片保存到用户相册
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              //保存成功失败之后，都要隐藏画板，否则影响界面显示。
              success: (res) => {
                console.log(res)
                wx.hideLoading()
                that.setData({
                  canvasHidden: true
                })
              },
              fail: (err) => {
                console.log(err)
                wx.hideLoading()
                that.setData({
                  canvasHidden: true
                })
              }
            })
          }
        })
      });
    }
})